package com.naihe.anderson.sms.widgets;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.activity.BaseSetActivity;
import com.naihe.anderson.sms.activity.NoteActivity;
import com.naihe.anderson.sms.activity.SaveSmsActivity;
import com.naihe.anderson.sms.activity.SearchActivity;
import com.naihe.anderson.sms.activity.SignatureMsgActivity;
import com.naihe.anderson.sms.util.MyAnimations;

public class PathMenuView extends RelativeLayout implements OnClickListener{

	private LinearLayout composerButtonsWrapper;
	private LinearLayout composer_buttons_layout;
	private ImageView composerButtonsShowHideButtonIcon;
	private ProgressBar reloadingProgress;

	private boolean areButtonsShowing;
	
	private Context context;
	public PathMenuView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		MyAnimations.initOffset(context);
		LayoutInflater inflater = LayoutInflater.from(context);
		inflater.inflate(R.layout.path_menu_layout, this, true);
	}
	
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		reloadingProgress = (ProgressBar) findViewById(R.id.reloading_progress);
		composerButtonsWrapper = (LinearLayout) findViewById(R.id.composer_buttons_wrapper);
		composer_buttons_layout = (LinearLayout) findViewById(R.id.composer_buttons_layout);
		
		composerButtonsShowHideButtonIcon = (ImageView) findViewById(R.id.composer_buttons_show_hide_button_icon);
		composerButtonsShowHideButtonIcon.setOnClickListener(this);
		
		for (int i = 0; i < composerButtonsWrapper.getChildCount(); i++) {
			composerButtonsWrapper.getChildAt(i).setOnClickListener(this);
		}
		
	}

	public void showProgress(){
		reloadingProgress.setVisibility(View.VISIBLE);
	}
	
	public void hideProgress(){
		if(reloadingProgress.isShown())
			reloadingProgress.setVisibility(View.GONE);
	}
	
	private void pathShow(){
		MyAnimations.startAnimationsIn(composer_buttons_layout, composerButtonsWrapper, 1000);
		Animation animation = MyAnimations.getRotateAnimation(0,-360, 900);
		animation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				composerButtonsShowHideButtonIcon.setImageResource(R.drawable.composer_button_2);
			}
		});
		composerButtonsShowHideButtonIcon.startAnimation(animation);
	}
	
	private void pathHide(){
		MyAnimations.startAnimationsOut(composer_buttons_layout, composerButtonsWrapper, 500);
		Animation animation = MyAnimations.getRotateAnimation(-360, 0, 400);
		animation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				composerButtonsShowHideButtonIcon.setImageResource(R.drawable.composer_button);
			}
		});
		composerButtonsShowHideButtonIcon.startAnimation(animation);
	}
	
	public void pathMenuDoing(){
		if (!areButtonsShowing) {
			pathShow();
		} else {
			pathHide();
		}
		areButtonsShowing = !areButtonsShowing;
	}
	
	@Override
	public void onClick(View v) {
		pathMenuDoing();
		
		if(v.getId() == R.id.composer_buttons_show_hide_button_icon){
			
		} else if(v.getId() == R.id.layout_001){
			Intent intent = new Intent(context, SearchActivity.class);
			context.startActivity(intent);
		} else if(v.getId() == R.id.layout_002){
			Intent intent = new Intent(context, SaveSmsActivity.class);
			context.startActivity(intent);
		} else if(v.getId() == R.id.layout_003){
			Intent intent = new Intent(context, BaseSetActivity.class);
			context.startActivity(intent);
		} else if(v.getId() == R.id.layout_004){
			Intent intent = new Intent(context, NoteActivity.class);
			context.startActivity(intent);
		} else if(v.getId() == R.id.layout_005){
		} else if(v.getId() == R.id.layout_006){
			Intent intent = new Intent(context, SignatureMsgActivity.class);
			context.startActivity(intent);
		}
	}
	
	public void starActivity(String packageName, String mainActivityName){
		ComponentName componet = new ComponentName(packageName, mainActivityName);
        Intent i = new Intent();
        i.setComponent(componet);
        context.startActivity(i);
        
        pathHide();
        areButtonsShowing = !areButtonsShowing;
	}
	
	public boolean isAreButtonsShowing() {
		return areButtonsShowing;
	}

	public void setAreButtonsShowing(boolean areButtonsShowing) {
		this.areButtonsShowing = areButtonsShowing;
	}
}
